﻿#region Using Directives
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using AppFabric.ServiceBus.Common;
using System.Xml.Linq;
using System.IO;
using System.Configuration;
#endregion

namespace AppFabric.ServiceBus.CommonForms
{
    public partial class ConnectForm : Form
    {
        #region Private Constants
        private const string SelectServiceBusNamespace = "Select a service bus namespace...";
        private List<NamespaceSettings> namespaceSettings;
        #endregion

        #region Public Constructor
        public ConnectForm()
        {
            InitializeComponent();
            PopulateNamespaces();
        }
        #endregion

        #region Public Properties
        public string Namespace { get; set; }
        public string ServicePath { get; set; }
        public string IssuerName { get; set; }
        public string IssuerKey { get; set; }
        #endregion

        #region Event Handlers
        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            Namespace = txtNamespace.Text;
            ServicePath = txtServicePath.Text;
            IssuerName = txtIssuerName.Text;
            IssuerKey = txtIssuerKey.Text;
            DialogResult = DialogResult.OK;
        }

        private void validation_TextChanged(object sender, EventArgs e)
        {
            btnOk.Enabled = !string.IsNullOrEmpty(txtNamespace.Text) &&
                            !string.IsNullOrEmpty(txtIssuerName.Text) &&
                            !string.IsNullOrEmpty(txtIssuerKey.Text);
        }

        #endregion

        #region Private Methods
        private List<NamespaceSettings> GetNamespacesSettings()
        {
            var name = ConfigurationManager.AppSettings["NamespaceConfigFilePath"].ToString();
            if (File.Exists(name))
            {
                XDocument document = XDocument.Load(name);
                var namespaces = from ns in document.Descendants("namespace")
                        select new NamespaceSettings
                        {
                            Namespace = ns.Attribute("name").Value,
                            Path = ns.Element("path").Value,
                            IssuerName = ns.Element("issuername").Value,
                            IssuerKey = ns.Element("issuerkey").Value
                        };
                return namespaces.ToList();
            }
            return null;
        }

        private void PopulateNamespaces()
        {
            try
            {
                namespaceSettings = GetNamespacesSettings();
                if (namespaceSettings != null)
                {
                    if (namespaceSettings.Count > 0)
                    {
                        cboServiceBusNamespace.DataSource = namespaceSettings;
                        var namespaceSetting = namespaceSettings.First();
                        SetNamespaceSetting(namespaceSetting);
                    }
                }
            }
            catch 
            {
               //Todo: To Log Errors 
            }
        }

        private void SetNamespaceSetting(NamespaceSettings namespaceSetting)
        {
            txtNamespace.Text = namespaceSetting.Namespace;
            txtIssuerName.Text = namespaceSetting.IssuerName;
            txtIssuerKey.Text = namespaceSetting.IssuerKey;
            txtServicePath.Text = string.IsNullOrEmpty(namespaceSetting.Path) ? string.Empty : namespaceSetting.Path;
        }

        private void cboServiceBusNamespace_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboServiceBusNamespace.SelectedItem != null)
            {
                var namespacesetting = (NamespaceSettings)cboServiceBusNamespace.SelectedItem; 
                SetNamespaceSetting(namespacesetting);
            }
        }
        #endregion
    }
}
